package DesignPattern.PublishAndSubscribe;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Description 主题 公众号
 */
public interface Subject {

    /**
     * @Description: 添加观察者（订阅者）到队列的方法
     * @Data:[observer]
     */
    void addToQueue(Observer observer);

    /**
     * @Description: 从队列移除
     * @Data:[observer]
     */
    void removeBeQueue(Observer observer);

    /**
     * @Description: 通知 观察者 发布消息
     */
    void notifyObserver();
}

class WeatherTencent implements Subject {
    String name;

    public WeatherTencent(String name,String content) {
        this.name = name;
        this.content = content;
    }

    //发布的内容
    private String content ;
    //关注者队列
    LinkedBlockingQueue<Observer> observers = new LinkedBlockingQueue<>();

    @Override
    public void addToQueue(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeBeQueue(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObserver() {
        observers.stream().forEach(v -> v.inform(this.name + "发布内容:" + content));
    }
}
